import { DTime, FormattedRes } from "@/hooks/useCountDown";
import dayjs from "dayjs";

export const calcTime = (time: DTime) => {
  if (!time) return 0;

  const res = dayjs(time).valueOf() - new Date().getTime(); //计算差值

  if (res < 0) return 0;

  return res;
};

export const parseMs = (milliseconds: number): FormattedRes => {
  return {
    days: Math.floor(milliseconds / 86400000),
    hours: Math.floor(milliseconds / 3600000) % 24,
    minutes: Math.floor(milliseconds / 60000) % 60,
    seconds: Math.floor(milliseconds / 1000) % 60,
    milliseconds: Math.floor(milliseconds) % 1000
  };
};
